package com.app.twth.net;

import android.util.Log;

import com.app.twth.bean.BaseResponseEntity;

import io.reactivex.observers.ResourceObserver;

/**
 * Description:
 * Encoding: utf-8
 */
public abstract class BaseObserver<T> extends ResourceObserver<T> {

    @Override
    public void onNext(T tJsonBase) {
//        System.out.println(tJsonBase.toString());
        Log.e("tag", " tJsonBase -->> " + tJsonBase.toString());
        try {
            BaseResponseEntity data = (BaseResponseEntity) tJsonBase;
            String code = data.getCode();
            if (code.equals("code")) {
                //TODO跳登录页
//                ZbbSpUtil.userLogout();
//                Intent intent = new Intent(ZApplication.getInstance(), LoginCompatActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                ZApplication.getInstance().startActivity(intent);
                return;
            }
            onData(tJsonBase);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public abstract void onData(T tJsonBase);

    @Override
    public void onComplete() {
        Log.e("tag", " onComplete");
    }

    @Override
    public void onError(Throwable e) {
        Log.e("tag", " onError getMessage-->> " + e.getMessage());
        Log.e("tag", " onError fillInStackTrace-->> " + e.fillInStackTrace());
        Log.e("tag", " onError toString-->> " + e.toString());
        HandlerException.ResponseThrowable throwable = HandlerException.handleException(e);
        onError(throwable.getMessage(), throwable.getCode());
    }

    public abstract void onError(String errorMsg, String code);
}

